﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace CRC.Comun.Presentacion
{
    public partial class VerFoto : Form
    {
        public VerFoto()
        {
            InitializeComponent();
        }

        public VerFoto(System.Drawing.Image pFoto,String pStrNombre,  String pStrcom)
            : this()
        {
            this.Text = pStrNombre;
            //this.Resize_image(pFoto);
            this.pictureBox1.Image = pFoto;
            this.toolTip1.SetToolTip(this.pictureBox1, pStrcom);
        }

        private void toolStripButtonGuardar_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
            {
                try
                {
                    FileStream stream = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                    EncoderParameters encoderParameters = new EncoderParameters(0);
                    pictureBox1.Image.Save(stream, GetEncoder(ImageFormat.Png), encoderParameters);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void toolStripButtonImprimir_Click(object sender, EventArgs e)
        {
            printDialog1.Document = printDocument1;
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
                printDocument1.Print();
            }
        }

        void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int alto = 10;
            int ancho = e.PageSettings.Bounds.Width;
            Font printFont = new Font("Courier New", 12);

            SizeF size = e.Graphics.MeasureString(this.Text, printFont);
            alto = alto + 10;
            e.Graphics.DrawString(this.Text, printFont, Brushes.Black, (ancho - size.Width) / 2, alto);
            
            alto = alto + 30;
            String comentario = toolTip1.GetToolTip(pictureBox1);
            size = e.Graphics.MeasureString(comentario, printFont);
            e.Graphics.DrawString(comentario, printFont, Brushes.Black, 30, alto);

            alto = alto + 30;
            e.Graphics.DrawImage(pictureBox1.Image, new Point((ancho - (int)pictureBox1.Image.PhysicalDimension.Width ) / 2 , alto));
        }

        private void toolStripButtonPrevImprimir_Click(object sender, EventArgs e)
        {
        }

        private ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

            foreach(ImageCodecInfo codec in codecs){
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }

        //private void Resize_image(System.Drawing.Image pFoto) 
        //{
        //    int width = 800;
        //    int heigh = 600;
        //    string ruta = "c:\\button.gif";
        //    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width,heigh);
        //    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)bmp);
        //    g.DrawImage(pFoto, 0, 0, width, heigh);
        //    bmp.Save(ruta);
        //    bmp.Dispose();
        //    this.pictureBox1.Image = System.Drawing.Image.FromFile(ruta);
        //    //this.pictureBox1.Image= 
        //}

    }
}
